package com.lb.jwitter.action;


 import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
 import java.util.List;
import java.util.Date;
import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import com.lb.jwitter.util.ApplicationContextProvider; 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 import com.opensymphony.xwork2.Action; 
 import java.util.Map; 
 import com.opensymphony.xwork2.ActionContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.service.TblQLDTTXADMKhoa;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHoc;
import com.lb.jwitter.service.TblQLDTTXADMKhoaHocService;
import com.lb.jwitter.service.TblQLDTTXADMKhoaService;
import com.lb.jwitter.service.TblQLDTTXADMNganh;
import com.lb.jwitter.service.TblQLDTTXADMNganhService;
import com.lb.jwitter.service.TblQLDTTXALop;
import com.lb.jwitter.service.TblQLDTTXALopService;
 import com.lb.jwitter.service.TblQuyenChiTietService;import com.lb.jwitter.service.TblTaiKhoan;import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhi;
 import com.lb.jwitter.service.TblQLDTTXAHocVienHocPhiService;
 import com.lb.jwitter.service.TblQLDTTXAHocVien;
 import com.lb.jwitter.service.TblQLDTTXAHocVienService;

import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXAHocVienHocPhiFormAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private List<TblQLDTTXAHocVienHocPhi> tblQLDTTXAHocVienHocPhis; 
    public List<TblQLDTTXAHocVienHocPhi>  getTblQLDTTXAHocVienHocPhis() {
         return tblQLDTTXAHocVienHocPhis; 
      } 

    private TblQLDTTXAHocVienHocPhiService tblQLDTTXAHocVienHocPhiService; 

    private List<TblQLDTTXAHocVien> tblQLDTTXAHocViens; 
    public List<TblQLDTTXAHocVien>  getTblQLDTTXAHocViens(){ 
              return tblQLDTTXAHocViens; 
     } 

    private TblQLDTTXAHocVienService tblQLDTTXAHocVienService; 

    private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam;
    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 
	 private TblQLDTTXADMKhoaService tblQLDTTXADMKhoaService;
	 private TblQLDTTXADMKhoaHocService tblQLDTTXADMKhoaHocService;
	 private List<TblQLDTTXADMKhoaHoc> tblQLDTTXADMKhoaHocs; 
	 private TblQLDTTXADMNganhService tblQLDTTXADMNganhService;
	 private TblQLDTTXALopService tblQLDTTXALopService;

	 
	 private List<TblQLDTTXALop> tblQLDTTXALops;
	 
	 private List<TblQLDTTXADMNganh> tblQLDTTXADMNganhs;
	 
	 private TblQLDTTXADMKhoa tblQLDTTXADMKhoa;
	 private Double diemDat;
	 private Long khoaId;
	 private Long khoaHocId;
	 private Long nganhId;
	 private Long hocKyId;
	 private String mSHV;
	 private String hoTenHV;
	 private String tenHV;
	 
	 
	 public String getTenHV() {
		return tenHV;
	}

	public void setTenHV(String tenHV) {
		this.tenHV = tenHV;
	}

	private Date today;
	 private List<TblQLDTTXADMKhoa> tblQLDTTXADMKhoas;

  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
	 
 public String execute() throws Exception 
{ 
 try{ 	
	     Map session = ActionContext.getContext().getSession();
	     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	     if( taiKhoanDangNhap == null) return "noprivilege";
	    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
	    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_HocVien_HocPhi", "xem")){ 
	        return "noprivilege"; 
	    }

	    
	    tblQLDTTXAHocVienHocPhiService = (TblQLDTTXAHocVienHocPhiService) context.getBean("tbl_QLDTTXA_HocVien_HocPhiClient"); 
	    tblQLDTTXAHocVienService = (TblQLDTTXAHocVienService) context.getBean("tbl_QLDTTXA_HocVienClient"); 
	
	    tblQLDTTXADMKhoaService = (TblQLDTTXADMKhoaService) context.getBean("tbl_QLDTTXA_DM_KhoaClient");

	    tblQLDTTXADMKhoaHocService = (TblQLDTTXADMKhoaHocService) context.getBean("tbl_QLDTTXA_DM_KhoaHocClient");

	    tblQLDTTXADMNganhService = (TblQLDTTXADMNganhService) context.getBean("tbl_QLDTTXA_DM_NganhClient");

	    tblQLDTTXALopService = (TblQLDTTXALopService) context.getBean("tbl_QLDTTXA_LopClient");

	    tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

       if(pageSize == 0){ 
            pageSize = 20; 
        } 
        if(page == 0){ 
            page =1; 
        } 
        
        
        Date today = new Date();
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        if(hocKyId !=null && hocKyId != 0){
        	tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNamService.gettblQLDTTXAHocKyTrongNam(hocKyId);
        }else {
        	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
        	ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1(df.format(today));
            con.setOperator("le");
            con.setType("date");
            cons.add(con);	
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setCol("thoiGianKetThuc");
            con2.setValue1(df.format(today));
            con2.setOperator("ge");
            con2.setType("date");
            cons.add(con2);	
            
            tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);

            if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() > 0){
            	tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
            }
        }
        
  	   

        cons.clear();
        if(tblQLDTTXAHocKyTrongNam != null){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
            con.setOperator("eq");
            cons.add(con);	
            
        }else{
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(new Long(1));
            con.setOperator("eq");
            cons.add(con);	
        }
        if(khoaId != null && !khoaId.equals(new Long(0))){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.khoaId.id");
            con.setValue1(khoaId);
            con.setOperator("eq");
            cons.add(con);		 
    	} else {
    		ConditionReport con = new ConditionReport(); 
	        con.setCol("hocVienId.khoaId.id");
	        con.setValue1(new Long(1));
	        con.setOperator("eq");
	        cons.add(con);		 
		}
        if(nganhId != null && !nganhId.equals(new Long(0))){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.nganhId.id");
            con.setValue1(nganhId);
            con.setOperator("eq");
            cons.add(con);		 
    	}
        if(khoaHocId != null && !khoaHocId.equals(new Long(0))){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.khoaHocId.id");
            con.setValue1(khoaHocId);
            con.setOperator("eq");
            cons.add(con);		 
    	}
        if(mSHV != null && !mSHV.equalsIgnoreCase("")){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.MSHV");
            con.setValue1("%" + mSHV + "%");
            con.setOperator("like");
            cons.add(con);	
        }
        if(hoTenHV != null && !hoTenHV.equalsIgnoreCase("")){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.hoTen");
            con.setValue1("%" + hoTenHV + "%");
            con.setOperator("like");
            cons.add(con);	
        }
        if(tenHV != null && !tenHV.equalsIgnoreCase("")){ 
            ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.ten");
            con.setValue1("%" + tenHV + "%");
            con.setOperator("like");
            cons.add(con);	
        } 
        {
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocVienId.trangThaiId.id");
            con.setValue1(new Long(1));
            con.setOperator("eq");
            cons.add(con);		 
    	}
        
        
        
        tblQLDTTXAHocVienHocPhis =        tblQLDTTXAHocVienHocPhiService.findtblQLDTTXAHocVienHocPhiByCondition(cons, pageSize, page); 
        pages = tblQLDTTXAHocVienHocPhiService.counttblQLDTTXAHocVienHocPhiByCondition(cons)/pageSize + 1; 

        cons.clear();
        
        //dm khoa
    	  if(khoaId != null && khoaId != 0){
    		  tblQLDTTXADMKhoa = tblQLDTTXADMKhoaService.gettblQLDTTXADMKhoa(khoaId);
    			 
    	  } else {
    		  khoaId = new Long(1);
    		    tblQLDTTXADMKhoa = tblQLDTTXADMKhoaService.gettblQLDTTXADMKhoa(new Long(1));
    	  }
    	  if(true){
    		    ConditionReport con = new ConditionReport(); 
    		    con.setCol("id");
    		    con.setValue1("desc");
    		    con.setOperator("order");
    		    cons.add(con);
    	  }
    	  tblQLDTTXADMKhoas = tblQLDTTXADMKhoaService.findtblQLDTTXADMKhoaByCondition(cons, 0, 0);
    	  
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findAlltblQLDTTXAHocKyTrongNam(0, 0);

		tblQLDTTXADMKhoaHocs = tblQLDTTXADMKhoaHocService.findtblQLDTTXADMKhoaHocByCondition(cons, 0, 0);
		tblQLDTTXADMNganhs = tblQLDTTXADMNganhService.findtblQLDTTXADMNganhByCondition(cons, 0, 0);

		dataString = ""; 
		if(tblQLDTTXAHocKyTrongNam != null)
			dataString += "&hocKyId=" + tblQLDTTXAHocKyTrongNam.getId() ;
		else
			dataString += "&hocKyId=" ;
		
		if(khoaHocId != null)
			dataString += "&khoaHocId=" + khoaHocId ;
		else
			dataString += "&khoaHocId=" ;
		
		if(nganhId != null)
			dataString += "&nganhId=" + nganhId  ;
		else
			dataString += "&nganhId=" ;
		
		if(khoaId != null)
			dataString += "&khoaId=" + khoaId ;
		else
			dataString += "&khoaId=" ;

	    dataString += "&mSHV=" + mSHV;
	    dataString += "&hoTenHV" + hoTenHV;

	
	      return Action.SUCCESS; 
      } 
      catch(Exception e){ 
      System.out.println("Loi Form tbl_QLDTTXA_HocVien_HocPhi Action"); 
     e.printStackTrace(); 
    return Action.ERROR; 
 } 
 }

public TblQLDTTXAHocKyTrongNam getTblQLDTTXAHocKyTrongNam() {
	return tblQLDTTXAHocKyTrongNam;
}

public void setTblQLDTTXAHocKyTrongNam(
		TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam) {
	this.tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNam;
}

public List<TblQLDTTXADMKhoaHoc> getTblQLDTTXADMKhoaHocs() {
	return tblQLDTTXADMKhoaHocs;
}

public void setTblQLDTTXADMKhoaHocs(
		List<TblQLDTTXADMKhoaHoc> tblQLDTTXADMKhoaHocs) {
	this.tblQLDTTXADMKhoaHocs = tblQLDTTXADMKhoaHocs;
}

public List<TblQLDTTXALop> getTblQLDTTXALops() {
	return tblQLDTTXALops;
}

public void setTblQLDTTXALops(List<TblQLDTTXALop> tblQLDTTXALops) {
	this.tblQLDTTXALops = tblQLDTTXALops;
}

public List<TblQLDTTXADMNganh> getTblQLDTTXADMNganhs() {
	return tblQLDTTXADMNganhs;
}

public void setTblQLDTTXADMNganhs(List<TblQLDTTXADMNganh> tblQLDTTXADMNganhs) {
	this.tblQLDTTXADMNganhs = tblQLDTTXADMNganhs;
}

public TblQLDTTXADMKhoa getTblQLDTTXADMKhoa() {
	return tblQLDTTXADMKhoa;
}

public void setTblQLDTTXADMKhoa(TblQLDTTXADMKhoa tblQLDTTXADMKhoa) {
	this.tblQLDTTXADMKhoa = tblQLDTTXADMKhoa;
}

public Long getKhoaId() {
	return khoaId;
}

public void setKhoaId(Long khoaId) {
	this.khoaId = khoaId;
}

public Date getToday() {
	return today;
}

public void setToday(Date today) {
	this.today = today;
}

public List<TblQLDTTXADMKhoa> getTblQLDTTXADMKhoas() {
	return tblQLDTTXADMKhoas;
}

public void setTblQLDTTXADMKhoas(List<TblQLDTTXADMKhoa> tblQLDTTXADMKhoas) {
	this.tblQLDTTXADMKhoas = tblQLDTTXADMKhoas;
}

public Long getKhoaHocId() {
	return khoaHocId;
}

public void setKhoaHocId(Long khoaHocId) {
	this.khoaHocId = khoaHocId;
}

public Long getNganhId() {
	return nganhId;
}

public void setNganhId(Long nganhId) {
	this.nganhId = nganhId;
}

public Long getHocKyId() {
	return hocKyId;
}

public void setHocKyId(Long hocKyId) {
	this.hocKyId = hocKyId;
} 
public String getMSHV(){
	return mSHV;
}
public void setMSHV(String mSHV){
	this.mSHV = mSHV;
}
public String getHoTenHV(){
	return hoTenHV;
}
public void setHoTenHV(String hoTenHV){
	this.hoTenHV = hoTenHV;
}
 } 
 
